package org.hyperzsoft.one;

import org.jsoup.helper.HttpConnection;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

public class predictor {
	private static final String PROXY_PWD = "H@xorz05";
	private static final String PROXY_UID = "siegleda";
	private static final int PROXY_PORT = 8080;
	private static final String PROXY_HOST = "internet2.can.eds.com";
	
	public ArrayList<Team> teams = null;
	private static final String USER_AGENT = "Mozilla";
	
	public predictor () {}
	
	public void startParse() {
		long timing;
		teams = new ArrayList<Team>();
		Calendar cal = Calendar.getInstance();
		
		timing = cal.getTimeInMillis();
		getCurrentStandingsData();
		cal = Calendar.getInstance();
		timing = cal.getTimeInMillis() - timing;
		
		System.out.println("Built standings in " + timing + " msecs");
//		
// 		for (team teamObj : teams) {
// 			timing = cal.getTimeInMillis();
//			getGameSchedule(teamObj);
//			cal = Calendar.getInstance();
//			timing = cal.getTimeInMillis() - timing;
//			
//			System.out.println("Built schedule for " + teamObj.fullName + " in " + timing + " msecs");
//			//for (Entry<String, OppTeam> oppTeam : teamObj.gameSchedule.entrySet()) {
//			//	OppTeam opTeam = ((OppTeam)oppTeam.getValue());
//			//	
//			//	getHistoricalData(teamObj.acroName, opTeam);
//			//}
//		}
	}
	
	public void getHistoricalData(String teamName, OppTeam opTeam) {
		Element histData = null;
		
		Document doc = getDocument("http://www.nhl.com/ice/schedulebyseason.htm", 
				"season", getSeasonValue(1, false), "gameType", "2", "team", 
				opTeam.getAcroName(), "network", "", "venue", "");
		
		//need to validate that we even have this index
		if (doc.body().select("tbody").size() >= 3) {
			histData = doc.body().select("tbody:has(td.team)").first();
			opTeam.BuildOpponentHistory(histData, teamName);
		}
	}
	
	public void getCurrentStandingsData() {
		Calendar cal = Calendar.getInstance();
		cal.roll(Calendar.DATE, true);
		SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
		
		Document doc = getDocument("http://www.nhl.com/ice/standings.htm", 
				"date", sdf.format(cal.getTime()), "season", 
				getSeasonValue(), "type", "LEA");
		
		Element standings = doc.body().select("tbody").get(2);
		
		for (Element teamRow : standings.select("tr")) {
			Team currTeam = new Team();
			currTeam.BuildTeamFromNode(teamRow);
			teams.add(currTeam);
		}
	}
	
	@SuppressWarnings("unused")
	private void getGameSchedule(Team teamObj) {
		Document doc = getDocument("http://www.nhl.com/ice/schedulebyseason.htm", 
				"season", getSeasonValue(), "gameType", "2", "team", 
				teamObj.acroName, "network", "", "venue", "");
		
		Element schedule = doc.body().select("tbody").get(2);
		
		teamObj.BuildSchedule(schedule);
	}
	
	public Document getDocument(String url, String... keyvals) {
		try {
			HttpConnection jsConn = HttpConnection.connect(url);
			
			jsConn.proxy(PROXY_HOST, PROXY_PORT, PROXY_UID, PROXY_PWD);
			jsConn.userAgent(USER_AGENT);
			jsConn.data(keyvals);
			return jsConn.get();
		} catch (IOException e) {
			//Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
	}
	
	public Document getDocument(String url) {
		try {
			HttpConnection jsConn = HttpConnection.connect(url);
			
			jsConn.proxy(PROXY_HOST, PROXY_PORT, PROXY_UID, PROXY_PWD);
			jsConn.userAgent(USER_AGENT);
			return jsConn.get();
		} catch (IOException e) {
			//Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
	}
	
	private String getSeasonValue() {
	    return getSeasonValue(0, true);
	}
	
	private String getSeasonValue(int years, boolean up) {
		int seasonYrEnd;
		int seasonYrStart;
		Calendar cal = Calendar.getInstance();
		
	    if (cal.get(Calendar.MONTH) >= Calendar.OCTOBER) 
	    	cal.roll(Calendar.YEAR, true);
	    
	    //if (years > 0) cal.roll(Calendar.YEAR, up);
	    
	    seasonYrEnd = cal.get(Calendar.YEAR);
	    
	    seasonYrEnd = (up) ? seasonYrEnd + years : seasonYrEnd - years;
	    
	    seasonYrStart = seasonYrEnd - 1;
	    
	    return Integer.toString(seasonYrStart) + Integer.toString(seasonYrEnd);
	}
}